﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Waker.Common;

namespace Waker.Cards
{
    public partial class FormConstructionEdit : Waker.Base.FormBasePop
    {

        DataTable _DtSource;
        public FormConstructionEdit(int id)
        {
            InitializeComponent();
            this.Text += id < 1 ? "添加" : "修改";
            _DtSource = new BAL.BAL_Cards().GetConstructionSite(id);
        }

        private void FormConstructionEdit_Load(object sender, EventArgs e)
        { 
            if (_DtSource.Rows.Count < 1)
                this.BindingContext[_DtSource].AddNew();
            this.BindingContext[_DtSource].EndCurrentEdit();
            ControlUtil.BindData(this.Controls, _DtSource);
        }
        protected override void FormSave()
        {
            if (_DtSource == null || _DtSource.Rows.Count < 1)
                return;
            base.errorPop.ClearErrors();
            if (string.IsNullOrEmpty(txtSite_No.Text))
            {
                base.errorPop.SetError(txtSite_No, "编号不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtSite_Name.Text))
            {
                base.errorPop.SetError(txtSite_Name, "名称不能为空");
                return;
            }
            this.BindingContext[_DtSource].EndCurrentEdit();
            if (_DtSource == null || _DtSource.Rows.Count < 1)
                return;
            var isOk = new BAL.BAL_Cards().SaveConstructionSite(_DtSource.Rows[0]);
            if (isOk)
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError("保存失败");
            }
        }

    }
}
